package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.system.domain.Instrumentation;
import com.ruoyi.system.mapper.InstrumentationMapper;
import com.ruoyi.system.service.IInstrumentationService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 仪器Service业务层处理
 * 
 * @author malang
 * @date 2022-02-01
 */
@Service
public class InstrumentationServiceImpl implements IInstrumentationService {
	private static final String belongType = FileTypeEnum.Instrument.getValue();
	@Autowired
	private InstrumentationMapper instrumentationMapper;
	@Autowired
	private ILabFileService labFileService;

	/**
	 * 查询仪器
	 * 
	 * @param instrumentationId
	 *            仪器ID
	 * @return 仪器
	 */
	@Override
	public Instrumentation selectInstrumentationById(Long instrumentationId) {
		List<LabFile> labFiles = labFileService.getFiles(instrumentationId, belongType);
		Instrumentation instrumentation = instrumentationMapper.selectInstrumentationById(instrumentationId);
		instrumentation.setImages(labFiles);
		return instrumentation;
	}

	/**
	 * 查询仪器列表
	 * 
	 * @param instrumentation
	 *            仪器
	 * @return 仪器
	 */
	@Override
	public List<Instrumentation> selectInstrumentationList(Instrumentation instrumentation) {
		return instrumentationMapper.selectInstrumentationList(instrumentation);
	}

	/**
	 * 新增仪器
	 * 
	 * @param instrumentation
	 *            仪器
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertInstrumentation(Instrumentation instrumentation) {
		int key = instrumentationMapper.insertInstrumentation(instrumentation);
		if(key == 0){
			return -1;
		}
		long instrumentationId = instrumentation.getInstrumentationId();
		labFileService.insertFiles(instrumentation.getImages(), instrumentationId, belongType);
		return key;
	}

	/**
	 * 修改仪器
	 * 
	 * @param instrumentation
	 *            仪器
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateInstrumentation(Instrumentation instrumentation) {
		List<LabFile> labFiles = instrumentation.getImages();
		labFileService.updateFiles(labFiles, instrumentation.getInstrumentationId(), belongType);
		return instrumentationMapper.updateInstrumentation(instrumentation);
	}

	/**
	 * 批量删除仪器
	 * 
	 * @param instrumentationIds
	 *            需要删除的仪器ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteInstrumentationByIds(Long[] instrumentationIds) {
		for (Long instrumentationId : instrumentationIds) {
			labFileService.deleteLabFile(instrumentationId, belongType);
		}
		return instrumentationMapper.deleteInstrumentationByIds(instrumentationIds);
	}

	/**
	 * 删除仪器信息
	 * 
	 * @param instrumentationId
	 *            仪器ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteInstrumentationById(Long instrumentationId) {
		labFileService.deleteLabFile(instrumentationId, belongType);
		return instrumentationMapper.deleteInstrumentationById(instrumentationId);
	}

}
